<?php

class Password extends Controller 
{
	function setVars()
	{
		$this->tpl['title'] = 'Password Change Form';
		$this->tpl['head'] = '';
        $this->tpl['body'] = '';
        $this->tpl['sidebar'] = '<h3 class="SideBar">Here you can change your password. Doing this frequently is good for security.</h3>';
        $this->uid = $this->session->userdata('uid');
	}
	
	function __construct()
	{
		parent::Controller();
        log_message('debug', "Control Panel->Password: Controler Class Initialized");
        $this->auth->CheckTaMeR(100,2);
		$this->setVars();
        $this->load->helper('form');
        //$this->load->library('profiler');
	}

	function index() 
	{
		if(isset($_POST['submit_pass']))
		{
			$this->postCheck();
		}
        $this->form();

	}

    	
    /**
     * @note	form
     * @info	Create Form in Table 
     * @author	Dennis T. Kaplan
     * @access	public
     * @param	 
     * @return	html
     */	
    public function form()
    {
        ((isset($this->validation->Password_error))?$class='FormErr':$class='');
        //$class='';if(isset($this->validation->Password_error))$class='FormErr';
        $form['Password'] = form_password(array(
                                    'name'        => 'Password',
                                    'id'          => 'Password',
                                    'value'       => '',
                                    'autocomplete'=> 'off',
                                    'maxlength'   => '20',
                                    'size'        => '10',
                                    'class'       => $class
                                    )
                                 );

        $class='';if(isset($this->validation->Confirm_error)){$class='FormErr';}
        $form['Confirm'] = form_password(array(
                                        'name'        => 'Confirm',
                                        'id'          => 'Confirm',
                                        'value'       => '',
                                        'autocomplete'=> 'off',
                                        'maxlength'   => '20',
                                        'size'        => '10',
                                        'class'       => $class
                                      )
                                 );
                                 
        $form[''] =          form_submit(array('class' => 'submit', 
                                    'name'        => 'submit_pass',
                                    'value'       => 'Change',
                                    'style'       => 'margin:10px'
                                    )
                                );
        $this->load->library('table');
        $attributes = array('name' => 'pass');
        $this->tpl['body'] .= '<br /><br /><br /><br />';
        $this->tpl['body'] .= '<fieldset><legend>Change Password</legend>';
        $this->tpl['body'] .= form_open('control_panel/password', $attributes);
        $this->tpl['body'] .= $this->table->form_table($form);
        $this->tpl['body'] .= '</form></fieldset>';
        $this->load->view('blank', $this->tpl);

    }

	function postCheck()
	{
		$this->load->library('validation');
        $rules['Password'] = 'trim|required|min_length[4]|max_length[20]|alpha_numeric';
        $rules['Confirm']  = 'trim|required|matches[Password]';
		$this->validation->set_rules($rules);
		if ($this->validation->run() != FALSE)
		{
            $data['Password'] = $this->validation->Password;
            $this->db->where('uid', $this->uid);
            $this->db->update('contact_user', $data);
            $this->msg->setMsg('Your new password is set');
		}
	}
    

}
?>
